package comcy.pj.goods.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DefaultGoodsDao implements GoodsDao {

    @Autowired
    private DataSource dataSource;


    @Override
    public List<Map<String, Object>> findGoods() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            //1.获取连接对象
            conn = dataSource.getConnection();
            //2.创建Statement对象
            stmt = conn.createStatement();
            //3.发送sql
            String sql = "select * from tb_goods";
            rs = stmt.executeQuery(sql);
            //4.处理结果集
            List<Map<String, Object>> list = new ArrayList<>();


            while (rs.next()) {//循环一次取一行，一行记录映射为一个map对象
                //方法映射1：
//                Map<String, Object> map = new HashMap<>();
//                map.put("id", rs.getInt(1));
//                map.put("name", rs.getString(2));
//                map.put("remark", rs.getString(3));
//                map.put("createdTime", rs.getTimestamp(4));
                //方法映射2：
//                ResultSetMetaData rsmd=rs.getMetaData();
//                Map<String, Object> map = new HashMap<>();
//                for (int i=1;i<=rsmd.getColumnCount();i++){
//                   map.put(rsmd.getColumnLabel(i),rs.getObject(i)) ;
//                }
//                list.add(map);
                //方法3：
                list.add(map(rs));
            }
            //5.释放资源
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);

        } finally {
            //方法1：释放资源
//            if (rs != null) try {
//                rs.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            if (stmt != null) try {
//                stmt.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            //这里的连接是返回到了池中
//            if (conn != null) try { conn.close(); } catch (Exception e) { e.printStackTrace();}

            close(rs,stmt,conn);
        }
    }

    //方法映射3：
    private Map<String, Object> map(ResultSet rs) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            map.put(rsmd.getColumnLabel(i), rs.getObject(i));
        }
        return map;
    }


    //方法2：释放资源
    private void close(ResultSet rs,Statement stmt,Connection conn){
        if(rs!=null)try{rs.close();}catch(Exception e){e.printStackTrace();}
        if(stmt!=null)try{stmt.close();}catch(Exception e){e.printStackTrace();}
        //这里的连接是返回到了池中
        if(conn!=null)try{conn.close();}catch(Exception e){e.printStackTrace();}

    }
}




